package com.example.demo.ui.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo.R;
import com.example.demo.presenter.ILoginPresenter;
import com.example.demo.presenter.impl.LoginPresenter;
import com.example.demo.ui.main.MainActivity;

/**
 * v层，只负责ui的操作，ui跟用户的交互，不负责逻辑，与p层接触
 * View <--> Presenter <--> Model
 */
public class LoginActivity extends AppCompatActivity implements ILoginView {

    private ILoginPresenter mainPresenter;

    private EditText usernameEditText;

    private EditText pwdEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mainPresenter = new LoginPresenter(this);

        usernameEditText = findViewById(R.id.editText_username);
        pwdEditText = findViewById(R.id.editText_pwd);

        // 点击登录
        findViewById(R.id.button_login).setOnClickListener(v -> {
            mainPresenter.login(usernameEditText.getText().toString(), pwdEditText.getText().toString());
        });
    }

    // IMainView接口的，给presenter调用
    @Override
    public void loginSuccess() {
        Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
        // 跳转到主界面
        startActivity(new Intent(this, MainActivity.class));
        // 结束当前Activity
        finish();
    }
    // IMainView接口的，给presenter调用
    @Override
    public void loginFailure() {
        Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();
    }
}